﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CallLogger
{
    
    public partial class Form1 : Form
    {
        private bool _start = false;
        private bool _exists = false;
        List<string> phonelist = new List<string>();
        
        public Form1()
        {
            InitializeComponent();
            listBox1.DataSource = phonelist;
        }

        private void BackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                TcpClient client = new TcpClient(textBox1.Text, 16366);
                NetworkStream stream = client.GetStream();
                Byte[] data = new Byte[256];
                String responseData = String.Empty;
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                stream.Close();
                client.Close();
                e.Result = responseData;
            }
            catch (Exception)
            {
                e.Result = "Check Allworx IP Adress, Connection refused";
            }
        }
        private void BackgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {  
            phonelist.Add(e.Result.ToString());
            listBox1.DataSource = null;
            listBox1.DataSource = phonelist;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.RunWorkerAsync();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_start == true)
            {
                if (!this.backgroundWorker1.IsBusy)
                {
                    label2.Text = "Background Worker Started";
                    this.backgroundWorker1.RunWorkerAsync();
                }
                else
                {
                    label2.Text = "Waiting for Call information";
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            if (_start == false)
            {
                button1.Text = "Stop";
                _start = true;
                textBox1.Enabled = false;
            }
            else
            {
                button1.Text = "Start";
                _start = false;
                textBox1.Enabled = true;
            }
               
        }
        public DateTime FromUnixTime(string unixTime)
        {
            long uniX = long.Parse(unixTime);
            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var epochConverted =  epoch.AddSeconds(uniX);
            return epochConverted.ToLocalTime();
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var _newCall = new Call();
                string s = listBox1.SelectedItem.ToString();
                string[] _lines = Regex.Split(s, ",");
            
                _newCall.CallID = _lines[0];
                _newCall.Item2 = _lines[1];
                _newCall.Item3 = _lines[2];
                _newCall.Item4 = _lines[3];
                _newCall.Item5 = _lines[4];
                _newCall.Item6 = _lines[5];
                _newCall.Item7 = _lines[6];
                _newCall.Item8 = _lines[7];
                _newCall.Item9 = _lines[8];
                _newCall.Item10 = _lines[9];
                _newCall.Item11 = _lines[10];
                _newCall.Item12 = _lines[11];
                _newCall.Item13 = _lines[12];
                _newCall.Item14 = _lines[13];
                _newCall.Item15 = _lines[14];
                _newCall.Item16 = _lines[15];
                _newCall.Item17 = _lines[16];
                _newCall.Item18 = _lines[17];
                _newCall.Item19 = _lines[18];

                lbItem1.Text = _newCall.CallID;
                
                lbItem3.Text = _newCall.Item3;
                lbItem4.Text = FromUnixTime(_newCall.Item4).ToString();
                lbItem5.Text = _newCall.Item5;
                lbItem6.Text = _newCall.Item6;
                lbItem7.Text = _newCall.Item7;
                lbItem8.Text = _newCall.Item8;
                lbItem9.Text = _newCall.Item9;
                lbItem10.Text = _newCall.Item10;
                lbItem11.Text = _newCall.Item11;
                lbItem12.Text = _newCall.Item12;
                lbItem13.Text = _newCall.Item13;
                lbItem14.Text = _newCall.Item14;
                lbItem15.Text = _newCall.Item15;

                if (_newCall.Item16 == "0")
                {
                    lbItem16.Text = "Answered";
                }
                else if (_newCall.Item16 == "1")
                {
                    lbItem16.Text = "Remote Disconnect";
                }
                else
                {
                    lbItem16.Text = "Local Disconnect";
                }
                lbItem17.Text = _newCall.Item17;
                lbItem18.Text = _newCall.Item18;
            }
            catch (Exception)
            {
                //If this fails it is because of an incomplete stream capture
            }
        }
    }
}
